package com.sure0000.basic.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 参考：https://blog.csdn.net/danchu/article/details/70238002
 * @author xuyouchang
 * Created on 2021/4/1.
 */
public class CglibProxy {
    public void test(){
        System.out.println("hello test1");
    }

    public void test2() {
        System.out.println("hello test2");
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibProxy.class);
        enhancer.setCallback(new MethodInterceptor() {

            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                System.out.println("before method run...");
                // 类中函数的执行
                Object result = proxy.invokeSuper(obj, args);
                System.out.println("after method run...");
                return result;
            }
        });
        CglibProxy cglibProxy = (CglibProxy) enhancer.create();
        cglibProxy.test();
        cglibProxy.test2();
    }
}
